//
//  ZpPictureCollectionView.m
//  WeiBo_OC
//
//  Created by MacBook Pro on 16/5/30.
//  Copyright © 2016年 MacBook Pro. All rights reserved.
//

#import "ZpPictureCollectionView.h"
#import "ZpPicture.h"
#define picCell @"picCell"
#define itemMargin 3
#define rowSize 3
#pragma mark - ZpPictureCollectionCell
@interface ZpPictureCollectionCell : UICollectionViewCell

@property (nonatomic, strong) ZpPicture *picture;
@property (nonatomic, strong) UIImageView *picView;
@end

@implementation ZpPictureCollectionCell


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self.picView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
    }
    return self;
}


- (UIImageView *)picView{
    if (!_picView) {
        _picView = [[UIImageView alloc] init];
        _picView.contentMode = UIViewContentModeScaleAspectFill;
        _picView.clipsToBounds = YES;
        [self.contentView addSubview:_picView];
    }
    return _picView;
}


- (void)setPicture:(ZpPicture *)picture{

    _picture = picture;
    [self.picView sd_setImageWithURL:picture.thumbnail_pic];
}

@end



#pragma mark - ZpPictureCollectionView
@interface ZpPictureCollectionView () <UICollectionViewDataSource>

@property (nonatomic, weak) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, assign) CGFloat itemWidth;
@end
@implementation ZpPictureCollectionView


#pragma mark - 构造方法
+ (instancetype)pictureCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    ZpPictureCollectionView *picView = [[ZpPictureCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    picView.collectionViewLayout = flowLayout;
    [picView registerClass:[ZpPictureCollectionCell class] forCellWithReuseIdentifier:picCell];
    

    return picView;
}

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if (self = [super initWithFrame:CGRectNull collectionViewLayout:layout]) {
        self.flowLayout = (UICollectionViewFlowLayout *)layout;
        
        self.dataSource = self;
    }
    return self;
}

- (void)setPics:(NSArray *)pics{
    _pics = pics;
    
    [self settingFlowLayout];
    CGSize size = [self calculatePictureViewSize];

    [self updateConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(size);
    }];

    [self reloadData];
}

- (CGSize)calculatePictureViewSize{
    
    self.flowLayout.itemSize =CGSizeMake(self.itemWidth, self.itemWidth);
    if (self.pics.count == 0) {
        return CGSizeZero;
    }
    if (self.pics.count == 1) {
        self.flowLayout.itemSize =CGSizeMake(284, 284);
        return CGSizeMake(300, 300);

    }
    if (self.pics.count == 4) {
        CGFloat w = itemMargin + self.itemWidth * 2 + margin * 2;
        return CGSizeMake(w, w);
    }
    
    CGFloat w = self.getScreenWidth;
    int row = ((int)self.pics.count - 1) / rowSize + 1;
    CGFloat h = self.itemWidth * row + (row - 1) * itemMargin + margin * 2;
    
    return CGSizeMake(w, h);

}


#pragma mark - 设置布局对象
- (void)settingFlowLayout{
    CGFloat item = ([UIScreen mainScreen].bounds.size.width - (2 * margin) - (rowSize -1) * itemMargin)/ rowSize;
    self.itemWidth = item;
    self.flowLayout.itemSize = CGSizeMake(item, item);
    self.flowLayout.minimumLineSpacing = itemMargin;
    self.flowLayout.minimumInteritemSpacing = itemMargin;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin);
    
}




#pragma mark - 数据源
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.pics.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZpPictureCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:picCell forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
    cell.picture = self.pics[indexPath.item];

    return cell;
}

@end


